package com.victualler.product.customer.service.impl;

import com.victualler.core.base.service.impl.BaseServiceImpl;
import com.victualler.product.customer.dto.CustomerInfoDto;
import com.victualler.product.customer.entity.CustomerAddressDao;
import com.victualler.product.customer.entity.CustomerDao;
import com.victualler.product.customer.entity.CustomerItemDao;
import com.victualler.product.customer.repository.CustomerQueryRepository;
import com.victualler.product.customer.repository.CustomerRepository;
import com.victualler.product.customer.service.CustomerAddressService;
import com.victualler.product.customer.service.CustomerItemService;
import com.victualler.product.customer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 17:03 2018/4/5
 * @Discription:  Dict字典类型对应的服务层
 */
@Service
public class CustomerServiceImpl extends BaseServiceImpl<CustomerDao,CustomerRepository,CustomerQueryRepository> implements CustomerService {

    @Autowired
    private CustomerAddressService customerAddressService;

    @Autowired
    private CustomerItemService customerItemService;

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param id
     * @Function 根据id获取所有信息
     */
    public CustomerInfoDto findInfoDto(Long id) {
        CustomerDao customerDao = this.getById(id);
        List<CustomerAddressDao> customerAddressDaoList = customerAddressService.getByParentId(id);
        List<CustomerItemDao> customerItemDaoList = customerItemService.getByParentId(id);
        return new CustomerInfoDto(customerDao,customerItemDaoList,customerAddressDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:58 on 2019/1/16.
     * @param openId 微信对应的id号
     * @return  对应的客户详细信息
     * @Function 根据微信唯一标识确定
     */
    public CustomerInfoDto findInfoDtoByOpenId(String openId) {
        List<CustomerDao> list = this.baseRepository.findByOpenId(openId);
        CustomerDao customerDao =  (list == null || list.size() == 0) ? null : list.get(0);
        if(customerDao == null ) {
            return null;
        }
        List<CustomerAddressDao> customerAddressDaoList = customerAddressService.getByParentId(customerDao.getId());
        List<CustomerItemDao> customerItemDaoList = customerItemService.getByParentId(customerDao.getId());
        return new CustomerInfoDto(customerDao,customerItemDaoList,customerAddressDaoList);
    }

    /**
     * @author Guoshichang
     * @since 13:58 on 2019/1/16.
     * @return  对应的客户详细信息
     * @Function 根据微信唯一标识确定
     */
    public CustomerInfoDto bindCustomerByWeiXin(CustomerDao customerDao) {
        customerDao.setBusinessType("BUT0001");//默认 自营
        customerDao.setLevel("B"); //默认 B类型
        customerDao.setImageId(112L); //默认淘食汇图标
        customerDao.setType("CUT0005"); //默认 零售终端店
        //保存
        this.save(customerDao);
        return new CustomerInfoDto(customerDao,new ArrayList<>(),new ArrayList<>());
    }

    /**
     * @author Guoshichang
     * @since 13:39 on 2018/7/7.
     * @param customerInfoDto 全部信息
     * @Function 保存大对象
     */
    @RequestMapping("/saveInfo")
    public void saveInfo(CustomerInfoDto customerInfoDto) {
        //保存
        if(customerInfoDto.getCustomerDao().getId() == null) {
            this.save(customerInfoDto.getCustomerDao());
            for (CustomerItemDao customerItemDao : customerInfoDto.getItemDetailDaoList()) {
                customerItemDao.setParentId(customerInfoDto.getCustomerDao().getId());
            }
            this.customerItemService.saveAll(customerInfoDto.getItemDetailDaoList());
            //更新
            for (CustomerAddressDao customerAddressDao : customerInfoDto.getCustomerAddressDaoList()) {
                customerAddressDao.setParentId(customerInfoDto.getCustomerDao().getId());
            }
            this.customerAddressService.saveAll(customerInfoDto.getCustomerAddressDaoList());
        } else {
            //更新
            this.save(customerInfoDto.getCustomerDao());
            this.customerItemService.updateList(customerInfoDto.getItemDetailDaoList(),customerInfoDto.getCustomerDao().getId());
            this.customerAddressService.updateList(customerInfoDto.getCustomerAddressDaoList(),customerInfoDto.getCustomerDao().getId());
        }
    }

    /**
     * @author Guoshichang
     * @since 12:42 on 2019/1/18.
     * @param name name
     * @return
     * @Function  根据地址信息更新地址
     */
    public CustomerAddressDao updateAddress(String name ,String phone,String address,Long customerId) {
        CustomerAddressDao old = this.customerAddressService.getByName(name, customerId);
        if (old == null) {
            old = new CustomerAddressDao();
        }
        old.setAddress(address);
        old.setName(name);
        old.setPhone(phone);
        old.setParentId(customerId);
        //保存
        this.customerAddressService.save(old);
        return old;
    }
}
